# Copyright 2025 Arm Limited and/or its affiliates.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

from typing import Any, List

import torch

import tosa_serializer as ts

from executorch.backends.arm.operators.node_visitor import (
    NodeVisitor,
    register_node_visitor,
)
from executorch.backends.arm.operators.operator_validation_utils import (
    validate_num_inputs,
)
from executorch.backends.arm.tosa.mapping import TosaArg


@register_node_visitor
class ToDimOrderCopyVisitor(NodeVisitor):
    """
    Implement the type cast functionality of _to_dim_order_copy.

    Other features like setting of the dim_order or moving a tensor to a
    different device are not supported.

    Also note that the node should not be quantized.
    """

    target = "dim_order_ops._to_dim_order_copy.default"

    tosa_specs = NodeVisitor.tosa_specs

    def define_node(
        self,
        node: torch.fx.Node,
        tosa_graph: Any,
        inputs: List[TosaArg],
        output: TosaArg,
    ) -> None:
        validate_num_inputs(self.target, inputs, 1)
        attr = ts.TosaSerializerAttribute()
        attr.CastAttribute()
        self._serialize_operator(
            node, tosa_graph, ts.Op.CAST, [inputs[0].name], [output.name], attr
        )
